<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { showToast } from 'vant';
import PageHeader from '@/components/common/PageHeader.vue';
const router = useRouter();
const phone = ref('');
const code = ref('');
const password = ref('');
const loading = ref(false);
const checked = ref(false);
const isSendingCode = ref(false);
const countdown = ref(0);
let timer = null;

const onSubmit = () => {
    if (!phone.value || !code.value || !password.value) {
        showToast('请完整填写注册信息');
        return;
    }

    loading.value = true;

    // 模拟注册请求
    setTimeout(() => {
        loading.value = false;
        router.push('/login');
        showToast('注册成功，请登录');
    }, 1000);
};

const sendVerificationCode = () => {
    if (isSendingCode.value) return;
    if (!phone.value) {
        showToast('请输入手机号码');
        return;
    }

    isSendingCode.value = true;
    countdown.value = 60;

    // 开始倒计时
    timer = setInterval(() => {
        countdown.value--;
        if (countdown.value <= 0) {
            clearInterval(timer);
            isSendingCode.value = false;
        }
    }, 1000);

    // 模拟发送验证码
    showToast('验证码已发送');
};

const goBack = () => {
    router.go(-1);
};
</script>

<template>
    <div class="register-page">
        <page-header title="用户注册" />
        <div class="register-content">
            <div class="form-area">
                <div class="input-item">
                    <van-field v-model="phone" type="tel" placeholder="手机号码" clearable />
                </div>
                <div class="input-item verification-code">
                    <van-field v-model="code" placeholder="验证码" clearable />
                    <div class="code-button" @click="sendVerificationCode">
                        {{ isSendingCode ? `${countdown}s` : '获取验证码' }}
                    </div>
                </div>
                <div class="input-item">
                    <van-field v-model="password" type="password" placeholder="设置密码" clearable>
                        <template #right-icon>
                            <van-icon name="eye-o" />
                        </template>
                    </van-field>
                </div>
            </div>

            <div class="agreement">
                <van-checkbox v-model="checked" shape="square" icon-size="14px">
                    我已阅读并同意<span class="link">《用户协议》</span>和<span class="link">《隐私政策》</span>
                </van-checkbox>
            </div>

            <div class="register-button">
                <van-button block type="primary" @click="onSubmit" :loading="loading">
                    注册
                </van-button>
            </div>
        </div>
    </div>
</template>

<style scoped>
.register-page {
    min-height: 100vh;
    background-color: #fff;
    padding-top: 44px;
}

.register-content {
    padding: 0 20px;
}

.form-area {
    margin-bottom: 30px;
}

.input-item {
    margin-bottom: 15px;
    border: 1px solid #eee;
    border-radius: 4px;
    position: relative;
}

.verification-code {
    display: flex;
}

.verification-code .van-field {
    flex: 1;
}

.code-button {
    padding: 0 15px;
    height: 44px;
    line-height: 44px;
    color: #0080FF;
    font-size: 14px;
    border-left: 1px solid #eee;
    white-space: nowrap;
}

.agreement {
    margin-bottom: 20px;
    font-size: 12px;
    color: #666;
}

.agreement .link {
    color: #0080FF;
}

.register-button {
    margin-bottom: 15px;
}

.register-button :deep(.van-button) {
    background-color: #0080FF;
    border-color: #0080FF;
    border-radius: 4px;
    height: 44px;
}
</style>